<?php

namespace app\plan\controller;

class News
{

    public function news()
    {
        $newsModel = new \app\common\model\News();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://www.bhzyxy.net/');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close($ch);
        $start = "<li class=\"clearfix\">";
        $end = "<div class=\"index-row3\">";
        $result = substr($result, strpos($result, $start));
        $result = substr($result, 0, strpos($result, $end));
        $result = substr($result, 0, strpos($result, "</ul>"));
        $pattern = "#/__local[\w+\/]*.[\w+\/]*#i";
        preg_match_all($pattern, $result, $image);
        $pattern = "#info[\d+\w+\/.]*#i";
        preg_match_all($pattern, $result, $link);
        $pattern = "#title=\"(.+)\">#i";
        preg_match_all($pattern, $result, $title);
        $image = json_encode($image);
        $image = substr($image, 1, strlen($image) - 2);
        $image = json_decode($image);
        $link = json_encode($link);
        $link = substr($link, 1, strLen($link) - 2);
        $link = json_decode($link);
        $title = json_encode($title);
        $title = substr($title, 1, strlen($title) - 2);
        $title = str_replace('title=\"', '', $title);
        $title = str_replace('\">', '', $title);
        $title = substr($title, 0, strpos($title, ']') + 1);
        $title = json_decode($title);
        for ($i = 0; $i < sizeof($image); $i++) {
            $data = array();
            $data['news_title'] = $title[$i];
            $data['news_image'] = 'http://www.bhzyxy.net' . $image[$i];
            $data['news_link'] = 'http://www.bhzyxy.net/' . $link[$i];
            $data['add_time'] = time();
            $newsModel->insert($data);
        }
    }

}
